<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png"/>
<link rel="apple-touch-startup-image" href="/apple-touch-startup-image.png"/>
<link rel="stylesheet" href="/css/sugar.css" type="text/css" />
<link rel="stylesheet" href="/css/style.css" type="text/css" />
<script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="/js/jquery.touchwipe.js"></script>
<script type="text/javascript" src="/js/sugar.js"></script>
<script type="text/javascript">
var undefined;
var checkinLink = undefined;

function checkin(event) {
  event.preventDefault();
  if (checkinLink == undefined) {
    event.stopImmediatePropagation();
    checkinLink = this;
    navigator.geolocation.getCurrentPosition(function(position) {
      var latitude = position.coords.latitude; 
      var longitude = position.coords.longitude;
      var position = latitude + '/' + longitude;
      var url = $(checkinLink).attr('href') + position;
      $(checkinLink).attr('href', url);
      $(checkinLink).click();
    }, function(error) {
      var position = '0/0';
      var url = $(checkinLink).attr('href') + position;
      $(checkinLink).attr('href', url);
      $(checkinLink).click();
    });
  }
  else {
    checkinLink = undefined;
  }
}

function changeAttribute() {
  var li = $(this).closest('li');
  var a = li.find('a.edit');
  if (!a.hasClass('inNote')) {
    a.addClass('inNote');
  }
}

function tickItem(event) {
  if ($(this).hasClass('ticked')) {
    event.preventDefault();
    return;
  }
  $(this).addClass('ticked'); 
  $(this).removeClass('unticked'); 
}

function toggleInNote() {
  if ($(this).hasClass('inNote')) {
    var attribute = $(this).closest('li').find('input.attribute');
    attribute.val('');
    attribute.attr('size', 3);
  }
  $(this).toggleClass('inNote');
}

$(document).ready(function() {
  $('body').sugar({ 
    url : '/action/index',
    submit : 'input.submit',
    confirm : 'a.delete',
    expand : 'input.attribute',
    onLoad : function() {
        $('a.checkin').click(checkin);
        $('a.unticked').click(tickItem);
        $('a.edit').click(toggleInNote);
        $('input.attribute').change(changeAttribute);
        $('a.edit').touchwipe({
          wipeLeft: function(source) { $(source).closest('li').find('a.delete').toggle(); },
          wipeRight: function(source) { $(source).closest('li').find('a.delete').toggle(); },
          min_move_x: 150,
          preventDefaultEvents: false
        });
        $('li a.delete').bind('confirm', function() {
          $(this).closest('li').hide();
        });
        $('li a.delete').click(function() {
          $(this).hide();
        });
        $('a#logout').click(function(event) { 
          $(this).unbind('click');
          event.stopImmediatePropagation();
//          window.location = $(this).attr('href');
//          event.stopImmediatePropagation();
//          event.preventDefault();
        });      
        $('a#maps').click(function(event) {
          $(this).unbind('click');
          event.stopImmediatePropagation();
        });
      },
  });
});
</script>
<title>Lists</title>
</head>
<body>
</body>
</html>
